package com.lc.user.express.my;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.user.express.BaseSecondActivity;
import com.lc.user.express.R;
import com.lc.user.express.adapter.StardAdapter;
import com.lc.user.express.adapter.StardandAdapter;
import com.lc.user.express.adapter.StaredTextAdapter;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetStardand;
import com.lc.user.express.model.MyStardModel;
import com.lc.user.express.model.MyStardandModel;
import com.lc.user.express.model.MyStardtextModel;
import com.lc.user.express.ordering.OnCityChangedListener;
import com.lc.user.express.ordering.SelectCityActivity;
import com.lc.user.express.view.MyListView;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/1.
 */
public class MyStardandActivity extends BaseSecondActivity {

    private MyListView list2_stardand;
    private MyListView list_stardand;
    private MyListView list1_stardand;
    private StardandAdapter stardandAdapter;
    private StardAdapter stardAdapter;
    private StaredTextAdapter stared_textAdapter;
    private TextView text_title;
    private LinearLayout lay_back;
    private List<MyStardandModel> list = new ArrayList<>();
    private List<MyStardModel> addlist = new ArrayList<>();
    private List<MyStardtextModel> infolist = new ArrayList<>();
    private TextView text_city;//定位城市
    public static OnCityChangedListener onCityChanged = null;//监听城市
    public LinearLayout lay_city;

    public static RefreshData onRefreshData = null;

    public interface RefreshData {
        void onRefresh(String cityId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stardand);
        setHeadLayoutVisibility(View.GONE);
        initview();


    }

    public void initview() {
        text_city = (TextView) findViewById(R.id.text_city);
        onCityChanged = new OnCityChangedListener() {
            @Override
            public void onCityChanged(String city, String cityId) {
                text_city.setText(TextUtils.isEmpty(city) ? "定位中..." : city);
                AppContext.myPreferences.setCity(city);
                AppContext.myPreferences.setCityId(cityId);
                AppContext.myPreferences.setIsShowCityDialog(false);
                getData(cityId);
            }
        };
        onRefreshData = new RefreshData() {
            @Override
            public void onRefresh(String cityId) {
                getData(AppContext.myPreferences.getCityId());
            }
        };
        text_city.setText(TextUtils.isEmpty(AppContext.myPreferences.getCity()) ? "定位中..." : AppContext.myPreferences.getCity());
        lay_city = (LinearLayout) findViewById(R.id.lay_city);
        lay_city.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show(SelectCityActivity.class);
            }
        });

        text_title = (TextView) findViewById(R.id.text_title);
        text_title.setText("收费标准");
        lay_back = (LinearLayout) findViewById(R.id.lay_back);
        lay_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        list_stardand = (MyListView) findViewById(R.id.list_stardand);
        list1_stardand = (MyListView) findViewById(R.id.textlist);
        list2_stardand = (MyListView) findViewById(R.id.list2_stardand);

        stardandAdapter = new StardandAdapter(cnt, list);
        stardAdapter = new StardAdapter(cnt, addlist);
        stared_textAdapter = new StaredTextAdapter(cnt, infolist);
        list_stardand.setAdapter(stardandAdapter);
        list1_stardand.setAdapter(stared_textAdapter);
        list2_stardand.setAdapter(stardAdapter);

        getData(AppContext.myPreferences.getCityId());


        Log.i("getcityid", AppContext.myPreferences.getCityId());
    }

    private void getData(String cityId) {
        new GetStardand(cityId, new AsyCallBack<GetStardand.Info>() {
            @Override
            public void onSuccess(String toast, int type, GetStardand.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                list.clear();
                addlist.clear();
                infolist.clear();

                list.addAll(info.list);
                stardandAdapter.updateListView(list);

                addlist.addAll(info.addlist);
                stardAdapter.updateListView(addlist);

                infolist.addAll(info.infolist);
                stared_textAdapter.updateListView(infolist);
            }
        }).execute(cnt);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (onCityChanged != null) onCityChanged = null;
    }
}
